import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/models/ReleaseProject.dart';
import 'package:flutter_app/models/business.dart';
import 'package:flutter_app/models/creatProject.dart';
import 'package:flutter_app/models/findFriendApl.dart';
import 'package:flutter_app/models/findUndertakeApl.dart';
import 'package:flutter_app/models/findUser.dart';
import 'package:flutter_app/models/ganttChart.dart';
import 'package:flutter_app/models/getFriendList.dart';
import 'package:flutter_app/models/index.dart';
import 'package:flutter_app/models/login.dart';
import 'package:flutter_app/models/register.dart';
import 'package:flutter_app/models/usrBusiness.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'Dio.dart';

class Global {
  static SharedPreferences _prefs;
  //static Profile profile = Profile();
  static Login login = Login();

  static Register register = Register();
  // 网络缓存对象
  //static NetCache netCache = NetCache();

  static bool UserInfo = false;
  //业务管理
  static List business = List();
  static UsrBusiness usrBusiness = UsrBusiness();
  static List Checkbox = [
    {'text': '机加工', 'value': false, 'ID': 1},
    {'text': '车削', 'value': false, 'ID': 2},
    {'text': '螺纹', 'value': false, 'ID': 3},
    {'text': '精密', 'value': false, 'ID': 4},
    {'text': '铣洗', 'value': false, 'ID': 5},
    {'text': '钻削', 'value': false, 'ID': 6},
    {'text': '机械装配', 'value': false, 'ID': 7},
  ];
  static List tempCheckbox = List<Map<String, dynamic>>.generate(
    Checkbox.length,
        (index) => Map<String, dynamic>.from(Checkbox[index]),
    growable: true,
  );
  //好友管理
  static FindFriendApl findFriendApl = FindFriendApl();
  static GetFriendList getFriendList = GetFriendList();
  //用户管理
  static FindUser findUser = FindUser();
  //发布项目
  static CreatProject creatProject = CreatProject();
  static ReleaseProject releaseProject = ReleaseProject();
  //工程工序管理
  static MatchingFriend matchingFriend = MatchingFriend();
  static FindUndertakeApl findUndertakeApl = FindUndertakeApl();
  static FindUserProjects findUserProjects = FindUserProjects();
  static FindUserProcedures findUserProcedures = FindUserProcedures();
  //最近的消息
  static List recentDisposeItem = List();
  static List recentDisposedItem = List();
  //甘特图
  static List<Project> dataInChart = List();
  static List<User> usersInChart = List();
  //发送申请
  static CompleteApplication completeApplication = CompleteApplication();
  static ReminderApplication reminderApplication = ReminderApplication();   //初始化全局信息，会在APP启动时执行
  //初始化全局信息，会在APP启动时执行
  static Future init() async {
    _prefs = await SharedPreferences.getInstance();
    var _login = _prefs.getString("login");
    if (_login != null) {
      try {
        login = Login.fromJson(jsonDecode(_login));
      } catch (e) {
        print(e);
      }
    }

    // 如果没有缓存策略，设置默认缓存策略

    //初始化网络请求相关配置
    EA.init();
  }

  // 持久化Profile信息
  static saveLogin() =>
      _prefs.setString("login", jsonEncode(login.toJson()));

}

class ProfileChangeNotifier extends ChangeNotifier {
  Login get _login => Global.login;
  @override
  void notifyListeners() {
    Global.saveLogin(); //保存Profile变更
    super.notifyListeners(); //通知依赖的Widget更新
  }
}